package com.fastloan.core.kuaixin.baidu;

import com.kawins.spring.ContextProperty;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author rankai
 *         createTime 2017-11-2017/11/15 15:24
 */
public class BaiDuModel {

    private static final Logger log = LoggerFactory.getLogger(BaiDuModel.class);

    private static final OkHttpClient client = new OkHttpClient();

    private static final String kuaixinServer = (String) ContextProperty.getContextProperty("kuaixin.server");

    private static final String api_key = (String) ContextProperty.getContextProperty("kuaixin.api_key");

    /**
     * 百度多头情况
     *
     * @param username
     * @param idCard
     * @param phone
     * @return
     */
    public static String multiScore(String username, String idCard, String phone) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String multiScore = (String) ContextProperty.getContextProperty("baidu.multiScore");
        StringBuffer st = new StringBuffer(kuaixinServer).append(multiScore).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        return getString(st);
    }

    /**
     * 百度黑产分
     *
     * @param username
     * @param idCard
     * @param phone
     * @return
     */
    public static String blackScore(String username, String idCard, String phone) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String multiScore = (String) ContextProperty.getContextProperty("baidu.blackScore");
        StringBuffer st = new StringBuffer(kuaixinServer).append(multiScore).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        return getString(st);
    }

    /**
     * 百度金融风险名单查询
     *
     * @param username
     * @param idCard
     * @param phone
     * @return
     */
    public static String risk(String username, String idCard, String phone) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String multiScore = (String) ContextProperty.getContextProperty("baidu.risk");
        StringBuffer st = new StringBuffer(kuaixinServer).append(multiScore).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        return getString(st);
    }

    /**
     * 百度黑中介分
     *
     * @param username
     * @param idCard
     * @param phone
     * @return
     */
    public static String blackAgentScore(String username, String idCard, String phone) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String blackAgentScore = (String) ContextProperty.getContextProperty("baidu.blackAgentScore");
        StringBuffer st = new StringBuffer(kuaixinServer).append(blackAgentScore).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        return getString(st);
    }

    /**
     * 百度金融信用分查询
     *
     * @param username
     * @param idCard
     * @param phone
     * @return
     */
    public static String creditScore(String username, String idCard, String phone) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String creditScore = (String) ContextProperty.getContextProperty("baidu.creditScore");
        StringBuffer st = new StringBuffer(kuaixinServer).append(creditScore).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        return getString(st);
    }

    /**
     * 百度用户LBS校验
     *
     * @param username
     * @param idCard
     * @param phone
     * @param homeCity
     * @return
     */
    public static String LBSInfo(String username, String idCard, String phone, String homeCity) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard) || StringUtils.isBlank(phone)) {
            log.info("百度用户信息校验参数错误,姓名/身份证号码/手机号不能为空");
            return null;
        }
        String LBSInfo = (String) ContextProperty.getContextProperty("baidu.BSInfo");
        StringBuffer st = new StringBuffer(kuaixinServer).append(LBSInfo).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username).append("&mobileNum=").append(phone);
        if (StringUtils.isNotBlank(homeCity)) {
            st.append("&homeCity=").append(homeCity);
        }
        return getString(st);
    }

    /**
     * 百度用户信息校验
     *
     * @param username
     * @param idCard
     * @param phone
     * @param cardId
     * @return
     */
    public static String userInfo(String username, String idCard, String phone, String cardId) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(idCard)) {
            log.info("百度用户信息校验参数错误,姓名或身份证号码不能为空");
            return null;
        }
        String userInfo = (String) ContextProperty.getContextProperty("baidu.userInfo");
        StringBuffer st = new StringBuffer(kuaixinServer).append(userInfo).append("?api_key=").append(api_key);
        st.append("&personalId=").append(idCard).append("&name=").append(username);
        if (StringUtils.isNotBlank(phone)) {
            st.append("&mobileNum=").append(phone);
        }
        if (StringUtils.isNotBlank(cardId)) {
            st.append("&cardId=").append(cardId);
        }
        return getString(st);
    }


    private static String getString(StringBuffer st) {
        Request request = new Request.Builder()
                .url(st.toString())
                .build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String string = response.body().string();
                log.debug("收到快信返回数据:{}", string);
                return string;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
